<template>
  <div class="box">
    <div class="logined" v-if="isLogin">
      <div class="top-nav">
        <div :class="num==1?'nav-item active':'nav-item'" @click.stop="changeTab(1)">
          <div class="item-text">订阅课程</div>
          <div class="item-dot"></div>
        </div>
        <div :class="num==2?'nav-item active':'nav-item'" @click.stop="changeTab(2)">
          <div class="item-text">收藏课程</div>
          <div class="item-dot"></div>
        </div>
      </div>
      <!---->
      <div class="gray"></div>
      <div class="courses-box" v-show="num==1">
        <div class="course-item" v-for="item in courseItem.data" :key="item.id">
          <div class="course-thumb">
            <img :src="item.thumb" />
          </div>
          <div class="course-body">
            <div class="course-title">{{item.title}}</div>
            <div class="at">订阅时间&nbsp;1 个月前</div>
          </div>
        </div>
      </div>
      <div class="courses-box" v-show="num==2">
        <div class="course-item" v-for="item in likeItem" :key="item.id">
          <div class="course-thumb">
            <img :src="item.thumb" />
          </div>
          <div class="course-body">
            <div class="course-title">{{item.title}}</div>
            <div class="at">订阅时间&nbsp;1 个月前</div>
          </div>
        </div>
      </div>
      <!---->
      <div class="drop">
        <img src="static/img/Spinload.aee4582d.gif" style="display: none;" />
        <span>已经到底了</span>
      </div>
    </div>
    <div class="container" v-else>
      <div class="empty-box">
        <div class="image-empty-item">
          <img src="@/assets/img/img-placeholder.png" />
        </div>
      </div>
      <div class="btn-login-box">
        <div class="btn-login" @click="goLogin()">登录查看</div>
      </div>
    </div>

    <!-- 底部导航栏开始 -->
    <KPFoot type="study"></KPFoot>
    <!-- 底部导航栏结束 -->
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

import KPFoot from "@/components/KPFoot.vue";

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {
    KPFoot
  },
  data() {
    //这里存放数据
    return {
      isLogin: false,
      courseItem: {},
      likeItem: [],
      num: 1
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    changeTab: function(num) {
      this.num = num;
    },
    timeCompute: function(pre_time) {
      let data = new Date();
      let pre_data = new Date(pre_time);
      let time = data.getTime();
      let pre_date = pre_data.getTime();
      let date = parseInt(time) - parseInt(pre_date);
    },
    goLogin: function() {
      this.$router.push({
        path: "/login",
        query: {
          url: window.location.href
        }
      });
    }
  },
  created() {
    let token = localStorage.getItem("h5-token");
    if (token) {
      this.isLogin = true;
    }
    this.$request.get("/api/v2/member/courses").then(res => {
      this.courseItem = res.data;
      // console.log(res.data);
    });
    this.$request.get("/api/v2/member/courses/like").then(res => {
      this.likeItem = res.data.data;
    });
  }
};
</script>

<style lang="less" scoped>
.box {
  .container {
    position: absolute;
    left: 0;
    top: 0;
    overflow-y: scroll;
    overflow-x: hidden;
    width: 100%;
    height: 100%;
    .empty-box {
      width: 100%;
      height: auto;
      float: left;
      text-align: center;
      padding-top: 1.333333rem;
      padding-bottom: 1.333333rem;
      .image-empty-item {
        display: inline-block;
        img {
          width: 3.2rem;
          height: 3.2rem;
        }
      }
    }

    .btn-login-box {
      width: 100%;
      height: auto;
      float: left;
      box-sizing: border-box;
      padding-left: 15px;
      padding-right: 15px;
      .btn-login {
        width: 100%;
        height: 46px;
        box-sizing: border-box;
        line-height: 46px;
        background: #3ca7fa;
        border-radius: 4px;
        float: left;
        text-align: center;
        color: #fff;
        font-size: 16px;
        font-weight: 500;
      }
    }
  }

  .logined {
    position: absolute;
    left: 0;
    top: 0;
    overflow-y: scroll;
    overflow-x: hidden;
    width: 100%;
    height: 100%;
    .top-nav {
      width: 100%;
      height: auto;
      float: left;
      box-sizing: border-box;
      position: sticky;
      top: 0;
      z-index: 999;
      display: flex;
      padding-top: 17px;
      padding-bottom: 8px;
      background-color: #fff;
      .nav-item.active {
        flex: 1;
        position: relative;
        .item-text {
          color: #333;
          font-weight: 600;
        }

        .item-dot {
          position: absolute;
          bottom: 0;
          left: 50%;
          margin-left: -5px;
          width: 10px;
          height: 3px;
          background-color: #3ca7fa;
          border-radius: 2px;
        }
      }

      .nav-item {
        flex: 1;
        position: relative;
        .item-text {
          width: 100%;
          height: auto;
          float: left;
          font-size: 16px;
          font-weight: 400;
          color: #666;
          line-height: 16px;
          text-align: center;
          padding-bottom: 9px;
        }

        .item-dot {
          position: absolute;
          bottom: 0;
          left: 50%;
          margin-left: -5px;
          width: 10px;
          height: 3px;
          background: #fff;
          border-radius: 2px;
        }
      }
    }
    .gray {
      float: left;
      width: 100%;
      height: 10px;
      background: #f3f6f9;
    }

    .courses-box {
      width: 100%;
      height: auto;
      float: left;
      background-color: #fff;
      box-sizing: border-box;
      padding: 15px;
      .course-item {
        width: 100%;
        height: auto;
        float: left;
        margin-bottom: 15px;
        display: flex;
        .course-thumb {
          margin-right: 10px;
          img {
            width: 120px;
            height: 90px;
            border-radius: 4px;
          }
        }

        .course-body {
          flex: 1;
          padding-top: 5px;
          .course-title {
            width: 100%;
            height: 40px;
            float: left;
            font-size: 15px;
            font-weight: 500;
            color: #171923;
            line-height: 20px;
            text-overflow: -o-ellipsis-lastline;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            line-clamp: 2;
            -webkit-box-orient: vertical;
            margin-bottom: 22px;
          }

          .at {
            width: 100%;
            height: auto;
            float: left;
            font-size: 12px;
            font-weight: 400;
            color: #999;
            line-height: 12px;
          }
        }
      }
    }
  }
}
.drop {
  display: inline-block;
  width: 100%;
  text-align: center;
  color: #cccccc;
  font-size: 14px;
  margin-top: 30px;
  margin-bottom: 30px;
}
</style>
